from pyb import Pin

import sensor, image, utime, time

thresholds = (100,100,0,0,0,0)

red_threshold=    (40, 55,  66,  81, 30, 55)
yellow_threshold= (83, 98, -20,  -8, 48, 87)
green_threshold=  (76, 93, -77, -55, 65, 86)

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.VGA)

sensor.set_windowing((240, 240))

sensor.skip_frames(time = 2000)

sensor.set_auto_gain(False)#自动增益

sensor.set_auto_whitebal(False)#白平衡

sensor.set_auto_exposure(False,2000)

clock = time.clock()#追踪帧率

#ROI= (10,16,216,106) #指定框大小

#while (True):

while (True):
        print('1')

        clock.tick()

        #statistics=img.get_statistics(roi=ROI)

        img = sensor.snapshot()

        #img.draw_rectangle(ROI) #画出指定的框

        blob1 = img.find_blobs([red_threshold])

        if blob1:
            #for b in blob1:
                #img.draw_rectangle(blob.rect())
                img.draw_string(0,0,'R',color= (255,0,0))
                sensor.snapshot().save("red.jpg")
                print('red')
                break
while (True):
        print('2')
        clock.tick()

        #statistics=img.get_statistics(roi=ROI)

        img = sensor.snapshot()

        #img.draw_rectangle(ROI) #画出指定的框

        blob2 = img.find_blobs([yellow_threshold])

        if blob2:
            #for b in blob2:
                #img.draw_rectangle(blob.rect())
                img.draw_string(0,0,'Y',color= (255,255,0))
                sensor.snapshot().save("yellow.jpg")
                print('yellow')
                break


while (True):
        print('3')
        clock.tick()

        #statistics=img.get_statistics(roi=ROI)

        img = sensor.snapshot()

        #img.draw_rectangle(ROI) #画出指定的框

        blob3 = img.find_blobs([green_threshold])
        if blob3:
            #for b in blob3:
                #img.draw_rectangle(blob.rect())
                img.draw_string(0,0,'G',color= (0,255,0))
                sensor.snapshot().save("green.jpg")
                print('green')
                break
